package com.mall.service.authority.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.commons.common.CommonPage;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsMenu;
import com.mall.model.vo.UmsMenuVo;
import com.mall.service.authority.mapper.UmsMenuMapper;
import com.mall.service.authority.service.UmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements UmsMenuService {
    @Autowired
    UmsMenuMapper umsMenuMapper;
    @Autowired
    private UmsMenuService umsMenuService;
    @Override
    public CommonPage listByPage(Integer pageNum, Integer pageSize, Long parentId) {
        Page<UmsMenu> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsMenu> qw=new LambdaQueryWrapper<>();
        qw.eq(UmsMenu::getParentId,parentId);
        Page<UmsMenu> umsMenuPage = umsMenuMapper.selectPage(page, qw);
        CommonPage commonPage = CommonPage.getPage(umsMenuPage);
        return commonPage;
    }

    @Override
    public List<UmsMenuVo> treeList() {
        // 1. 查询所有菜单列表
        List<UmsMenu> allMenu = umsMenuService.getAll();

        // 2. 定义根节点菜单集合
        List<UmsMenuVo> rootMenus=new ArrayList<>();
        for (UmsMenu umsMenu : allMenu) {
            // 2.1 判断 parentId 是否为 0, 0 则是根节点
            if (umsMenu.getParentId()==0) {
                UmsMenuVo umsMenuVo=new UmsMenuVo();
                BeanUtil.copyProperties(umsMenu,umsMenuVo);
                rootMenus.add(umsMenuVo);
            }
        }
        // 3. 遍历根节点,为其添加子节点 children
        for (UmsMenuVo menu : rootMenus) {
            List<UmsMenuVo> children=getChild(menu.getId(),allMenu);
            menu.setChildren(children);
        }
        return rootMenus;
    }

    @Override
    public List<UmsMenu> getAll() {
        return umsMenuMapper.getAll();
    }


    @Override
    public CommonResult updateHidden(Integer id, Integer hidden) {
        return CommonResult.success(umsMenuMapper.updateHiddenById(id,hidden)) ;
    }

    @Override
    public CommonResult getById(Integer id) {
        UmsMenu umsMenu = umsMenuMapper.selectById(id);
        return CommonResult.success(umsMenu) ;
    }

    @Override
    public void updateOne(Long id, UmsMenu umsMenu) {
        if(umsMenu.getParentId() == 0){
            umsMenu.setLevel(0);
        }else {
            umsMenu.setLevel(1);
        }
        umsMenuMapper.updateById(umsMenu);
    }

    private List<UmsMenuVo> getChild(Long id, List<UmsMenu> allMenu) {
        // 定义子节点集合
        List<UmsMenuVo> childList=new ArrayList<>();
        // 如果 传来的 id 和 parentId 一样,则添加进 childList 中
        for (UmsMenu menu : allMenu) {
            if (id.equals(menu.getParentId())) {
                UmsMenuVo menuVo=new UmsMenuVo();
                BeanUtil.copyProperties(menu,menuVo);
                childList.add(menuVo);
            }
        }

        // 如果 childList 大小为0,说明没有子节点,则返回,结束递归
        if (childList.size()==0) {
            return childList;
        }

        // 不为0,递归
        for (UmsMenuVo umsMenuVo : childList) {
            umsMenuVo.setChildren(getChild(umsMenuVo.getId(), allMenu));
        }
        return childList;
    }
}
